﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Reviewer {
    public partial class FrmFavGamesDatabase : Form {
        private FrmMain frmMain;
        private DBHelper dbHelper = new DBHelper();

        public FrmFavGamesDatabase(FrmMain frmMain) {
            InitializeComponent();
            this.Icon = Icon.FromHandle(Properties.Resources.star.GetHicon());
            this.frmMain = frmMain;
            dgvGames.AutoGenerateColumns = false;
        }

        private void FrmFavGamesDatabase_Load(object sender, EventArgs e) {
            this.Text = "Favourite Games Database (" + dbHelper.GetTotalFavGamesCount() + " games)";
            cbxGameType.SelectedIndex = 0;
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e) {
            if(dgvGames.CurrentCell != null) {
                if(MessageBox.Show("Are you sure to delete " + dgvGames.CurrentRow.Cells["BSID"].Value.ToString() + "?", "Cofirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK) {
                    dbHelper.DeleteFavGame(dgvGames.CurrentRow.Cells["BSID"].Value.ToString());
                    dgvGames.DataSource = dbHelper.FilterFavGames(tbxKeyword.Text.Trim(), cbxGameType.Text.ToLower());
                    this.Text = "Favourite Games Database (" + dgvGames.Rows.Count + " games)";
                }
            }
        }

        private void cbxGameType_SelectedIndexChanged(object sender, EventArgs e) {
            if(tbxKeyword.Text.Trim() != "") {
                dgvGames.DataSource = dbHelper.FilterFavGames(tbxKeyword.Text.Trim(), cbxGameType.Text.ToLower());
            } else {
                dgvGames.DataSource = dbHelper.GetAllFavGames(cbxGameType.Text.ToLower());
            }
            this.Text = "Favourite Games Database (" + dgvGames.Rows.Count + " games)";
        }

        private void tbxKeyword_KeyDown(object sender, KeyEventArgs e) {
            if(e.KeyCode == Keys.Enter) {
                e.SuppressKeyPress = true;
            }
        }

        private void dgvGames_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            if(e.ColumnIndex > 0) {
                if(e.ColumnIndex == 3) {
                    if(dgvGames.Rows[e.RowIndex].Cells[5].Value.ToString() == "1-0") {
                        e.CellStyle.ForeColor = Color.Red;
                        return;
                    }
                } else if(e.ColumnIndex == 4) {
                    if(dgvGames.Rows[e.RowIndex].Cells[5].Value.ToString() == "0-1") {
                        e.CellStyle.ForeColor = Color.Red;
                        return;
                    }
                }
            }
        }

        private void tbxKeyword_KeyUp(object sender, KeyEventArgs e) {
            if(e.KeyCode == Keys.Enter) {
                if(tbxKeyword.Text.Trim() != "") {
                    dgvGames.DataSource = dbHelper.FilterFavGames(tbxKeyword.Text.Trim(), cbxGameType.Text.ToLower());
                } else {
                    dgvGames.DataSource = dbHelper.GetAllFavGames(cbxGameType.Text.ToLower());
                }
                this.Text = "Local Games Database (" + dgvGames.Rows.Count + " games)";
                e.SuppressKeyPress = true;
                return;
            }
        }

        private void dgvGames_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            if(e.RowIndex >= 0) {
                StringBuilder hgn = new StringBuilder();
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["BSID"].Value + " ");
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["GameType"].Value + " ");
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["WhitePlayer"].Value + " ");
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["BlackPlayer"].Value + " ");
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["GameResult"].Value + " ");
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["WhiteTime"].Value + " ");
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["BlackTime"].Value + " ");
                hgn.Append(dgvGames.Rows[e.RowIndex].Cells["Moves"].Value);

                frmMain.LoadDecodedHGNX(hgn.ToString());
            }
        }
    }
}
